package devgui;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import abstractgame.GamePiece;

import chess.ChessState;

public class DevChessPanel extends JPanel implements Observer {
	
	private int cellSize = 10;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ChessState board;
	
	public DevChessPanel(ChessState c, int size){
		super();
		board = c;
		
		cellSize = size/board.getBoardWidth();
		
		c.addObserver(this);
	}
	
	public void update(Observable arg0, Object arg1) {
		this.repaint();
		System.out.println("Panel is updated");
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		System.out.println("paint component is init");
		System.out.println(board.getBoardWidth());
		g.setColor(Color.WHITE);
		
		for(int x =0; x< board.getBoardWidth();x++){
			if (g.getColor() == Color.WHITE){
				g.setColor(Color.BLACK);
			}else{
				g.setColor(Color.WHITE);
			}
			for(int y =0; y< board.getBoardHeight();y++){
				
				if (g.getColor() == Color.WHITE){
					g.setColor(Color.BLACK);
				}else{
					g.setColor(Color.WHITE);
				}
				
				g.fillRect(x*cellSize, y*cellSize, cellSize, cellSize);
				System.out.println("Rect is drawn");
			}
		}
		
		paintPieces(g);
	}

	/**
	 * Draws the pieces to the screen
	 * @param g
	 */
	private void paintPieces(Graphics g) {
		System.out.println("Piece is drawn");
		GamePiece p;
		for(int i = 0; i<board.getWhitePieces().size();i++){
				p = board.getWhitePieces().get(i);
				// Image is not shown for some reason, could be blank.....
				g.drawImage(p.getImg(), p.getPos()[0]*10, p.getPos()[1]*10,null );
				g.fillRect(p.getPos()[0]*10, p.getPos()[1]*10, 100, 100);
		}
		
	}
}
